import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:wanflutter/manager/app_manager.dart';
import 'package:wanflutter/ui/page/page_login.dart';

import '../../event/events.dart';
import '../../http/api.dart';
import '../page/page_collect.dart';

class MainDrawer extends StatefulWidget { // 具有setState函数，数据变化，刷新UI时调用，stateless则没有更新的接口
  @override
  State<StatefulWidget> createState() {
    return _MainDrawerState();
  }
}

class _MainDrawerState extends State<MainDrawer> {

  String? _username;


  @override
  void initState() {
    super.initState();
    // 监听特定事件: 登录
    AppManager.eventBus.on<LoginEvent>().listen((event) {
      setState(() {
        _username = event?.username;
        // sp
        AppManager.prefs.setString(AppManager.ACCOUNT, _username!);
      });
    });
    _username = AppManager.prefs.getString(AppManager.ACCOUNT);
  }

  void _itemClick(Widget? page) {
    var dstPage = null == _username ? LoginPage() : page;
    // 如果参数不为空 则跳转
    if (dstPage != null) {
      Navigator.push(context, new MaterialPageRoute(builder: (context) {
        return dstPage;
      }));
    }
  }
  @override
  Widget build(BuildContext context) {
    // 侧滑菜单的顶部
    Widget userHeader = DrawerHeader(
      decoration: BoxDecoration(  //背景颜色
        color: Colors.blue
      ),
      child: InkWell(
        // 点击进入登录界面
        onTap: () => _itemClick(null),
        child: Column(
          children: <Widget>[
            Padding(
                padding: EdgeInsets.only(bottom: 18.0),
                child: CircleAvatar(
                  // 报错无法加载，需要在yaml中注册资源路径，才可被调用
                  backgroundImage: AssetImage("assets/images/logo.png"),
                  radius: 38.0,
                ),
            ),
            Text(
              _username ?? "请先登录",
              style: TextStyle(color: Colors.white, fontSize: 18.0),
            )
          ],
        ),
      ),
    );
    return ListView(
      // 不设置会导致状态栏失色
      padding: EdgeInsets.zero,
      children: [
        // 头部 账号栏菜单
        userHeader,

        //收藏
        InkWell(
          onTap: () => _itemClick(CollectPage()),
          child: ListTile(
            leading: Icon(Icons.favorite),
            title: Text('收藏列表', style: TextStyle(fontSize: 16.0),),
          ),
        ),
        Padding(
          padding: EdgeInsets.fromLTRB(18.0, 0.0, 18.0, 0.0),
          child: Divider(
            color: Colors.grey,
          ),
        ),
        // 推出登录
        Offstage(
          offstage: _username == null,
          child: InkWell(
            onTap: (){
              setState(() {
                AppManager.prefs.remove(AppManager.ACCOUNT);
                Api.clearCookie();
                _username = null;
              });
            },
            child: ListTile(
              leading: Icon(Icons.exit_to_app),
              title: Text('退出登录', style: TextStyle(fontSize: 16.0),)
            ),
          ),
        )
      ],
    );
  }

}